package com.example.distillapplication.hardware;

import com.example.x6.serial.SerialPort;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialManager {
    private SerialPort serialttyS1;//一号串口
    private InputStream ttyS1InputStream;
    private OutputStream ttyS1OutputStream;

    private SerialPort serialttyS2;//二号串口
    private InputStream ttyS2InputStream;
    private OutputStream ttyS2OutputStream;

    private SerialPort serialttyS3;//三号串口
    private InputStream ttyS3InputStream;
    private OutputStream ttyS3OutputStream;

    private SerialPort serialttyS4;//四号串口
    private InputStream ttyS4InputStream;
    private OutputStream ttyS4OutputStream;

    private static SerialManager INSTANCE;
    public static synchronized SerialManager getInstance(){
        if(INSTANCE==null){
            INSTANCE = new SerialManager();
        }
        return INSTANCE;
    }


    public SerialManager() {
    }

    //builder模式
    public SerialManager setSerialPort1BaudRate(int baudRate){
        try {
            serialttyS1 = new SerialPort(new File("/dev/ttyS1"),baudRate,0);
            ttyS1InputStream = serialttyS1.getInputStream();
            ttyS1OutputStream = serialttyS1.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return INSTANCE;
    }

    public void ttyS1Send(byte[] bytes) throws IOException {
        ttyS1OutputStream.write(bytes);
    }

    public void ttyS1Recieve(byte[] bytes) throws IOException {
        ttyS1InputStream.read(bytes);
    }

    public SerialManager setSerialPort2BaudRate(int baudRate){
        try {
            serialttyS2 = new SerialPort(new File("/dev/ttyS2"),baudRate,0);
            ttyS2InputStream = serialttyS2.getInputStream();
            ttyS2OutputStream = serialttyS2.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return INSTANCE;
    }

    public void ttyS2Send(byte[] bytes) throws IOException {
        ttyS2OutputStream.write(bytes);
    }

    public void ttyS2Recieve(byte[] bytes) throws IOException {
        ttyS2InputStream.read(bytes);
    }

    public SerialManager setSerialPort3BaudRate(int baudRate){
        try {
            serialttyS3 = new SerialPort(new File("/dev/ttyS3"),baudRate,0);
            ttyS3InputStream = serialttyS3.getInputStream();
            ttyS3OutputStream = serialttyS3.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return INSTANCE;
    }

    public void ttyS3Send(byte[] bytes) throws IOException {
        ttyS3OutputStream.write(bytes);
    }

    public void ttyS3Recieve(byte[] bytes) throws IOException {
        ttyS3InputStream.read(bytes);
    }

    public SerialManager setSerialPort4BaudRate(int baudRate){
        try {
            serialttyS4 = new SerialPort(new File("/dev/ttyS4"),baudRate,0);
            ttyS4InputStream = serialttyS4.getInputStream();
            ttyS4OutputStream = serialttyS4.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return INSTANCE;
    }

    public void ttyS4Send(byte[] bytes) throws IOException {
        ttyS4OutputStream.write(bytes);
    }

    public void ttyS4Recieve(byte[] bytes) throws IOException {
        ttyS4InputStream.read(bytes);
    }
}
